import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: hp
 * Date: 2024-05-12
 * Time: 15:46
 */
public class Sort {
    private static void swap(int[] array,int i,int j) {
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }
    public static void  insertSort(int[] array) {
         for(int i = 1; i < array.length; i++) {
             int tmp = array[i];
             int j = i - 1;
             for(; j >= 0; j--) {
                 if(array[j] > tmp) {//必须使用tmp来进行比较不能使用array[i]来进行比较，因为array[i]的数据可能会被前面的数据覆盖
                     array[j+1] = array[j];
                 }else {
                     break;
                 }
             }
             array[j+1] = tmp;
         }
    }

    public static void shell(int[] array) {
        int gap = array.length;
        while(gap > 1) {
            gap = gap / 3 + 1;
            for(int i = gap; i < array.length; i++) {
                int tmp = array[i];
                int j = i - gap;
                for(; j >= 0; j -= gap) {
                    if(array[j] > tmp) {
                        array[j + gap] = array[j];
                    }else {
                        break;
                    }
                }
                array[j+gap] = tmp;
            }
        }
    }


    public static void selectSort1(int[] array) {
        for(int i = 0; i < array.length; i++) {
            int min = i;
            int j = i;
            for(; j < array.length; j++) {
                if(array[j] < array[min]) {
                    min = j;
                }
            }
            swap(array,i,j);
        }
    }


    public static void selectSort2(int[] array) {
        for(int i = 0; i < array.length; i++) {
            int min = i;
            int max = i;
            int j = i+1;
            for(; j < array.length-i; j++) {
                if(array[j] > array[max]) {
                    max = j;
                }
                if(array[j] < array[min]) {
                    min = j;
                }
            }
            swap(array,min,i);
            if(max == i) {
                max = min;
            }
            if(array.length-1-i > max) {
                swap(array,max,array.length-1-i);
            }

        }
    }



    public static void bubbleSort(int[] array) {
        for(int i = 0; i < array.length-1; i++) {
            int flag = 1;
            for(int j = 0; j < array.length-i-1; j++) {
                if(array[j] > array[j+1]) {
                    swap(array,j,j+1);
                    flag = 0;
                }
            }
            if(flag == 1) {
                break;
            }
        }
    }

    public static void quickSort(int[] array) {
        quick(array,0,array.length-1);
    }

    public static void quick(int[] array,int start,int end) {
        if(start >= end) {
            return;
        }

        if(end - start + 1 <= 10) {
            insertSortquick(array,start,end);
            return;
        }

        int index = midTreeNum(array,start,end);
        swap(array,index,start);

        int patition = patition(array,start,end);
        quick(array,start,patition-1);
        quick(array,patition+1,end);

    }

    private static void insertSortquick(int[] array,int left, int right) {
        for(int i = left + 1; i <= right; i++) {
            int key = array[i];
            int j = i - 1;
            for(; j >= left; j--) {
                if(array[j] > key) {
                    array[j+1] = array[j];
                }else {
                    break;
                }
            }
            array[j+1] = key;
        }
    }

    private static int midTreeNum(int[] array,int left,int right) {
        int mid = (left + right) / 2;
        if(array[left] > array[right]) {
            if(array[right] > array[mid]) {
                return right;
            }else if(array[left] < array[mid]) {
                return left;
            }else {
                return mid;
            }
        }else {//array[left] <= array[right]
            if(array[left] > array[mid]) {
                return left;
            }else if(array[right] < array[mid]) {
                return right;
            }else {
                return mid;
            }
        }
    }

    public static int patitionHoare1(int[] array,int left,int right) {
        int i = left;
        while(left < right) {
            while(left < right && array[right] >= array[i]) {
                right--;
            }
            swap(array,right,i);
            i = right;
            while(left < right && array[i] >= array[left]) {
                left++;
            }
            swap(array,i,left);
            i = left;
        }
        return i;
    }


    public static int patitionHoare(int[] array,int left,int right) {
        int i = left;
        int tmp = array[left];
        while(left < right) {
            while(left < right && array[right] >= tmp) {
                right--;
            }
            while(left < right && array[left] <= tmp) {
                left++;
            }
            swap(array,right,left);
        }
        swap(array,left,i);
        return left;
    }

    public static int patition(int[] array,int left,int right) {
        int key = array[left];
        int i = left;
        while(left < right) {
            while(left < right && array[right] >= key) {
                right--;
            }
            array[i] = array[right];
            i = right;
            while(left  < right && array[left] <= key) {
                left++;
            }
            array[i] = array[left];
            i = left;
        }
        array[left] = key;
        return left;
    }

    public static int patition1(int[] array,int left,int right) {
        int key = array[left];
        while(left < right) {
            while(left < right && array[right] >= key) {
                right--;
            }
            array[left] = array[right];
            while(left  < right && array[left] <= key) {
                left++;
            }
            array[right] = array[left];
        }
        array[left] = key;
        return left;
    }


    public static void quickSortNor(int[] array) {
        int left = 0;
        int right = array.length-1;
        int par = patition(array,left,right);
        Stack<Integer> stack = new Stack<>();
        if(par > left + 1) {
            stack.push(left);
            stack.push(par-1);
        }
        if(par + 1 < right) {
            stack.push(par + 1);
            stack.push(right);
        }

        while(!stack.isEmpty()) {
           right = stack.pop();
           left = stack.pop();
           par = patition(array,left,right);
            if(par > left + 1) {
                stack.push(left);
                stack.push(par-1);
            }
            if(par + 1 < right) {
                stack.push(par + 1);
                stack.push(right);
            }
        }

    }

    public static void mergeSort(int[] array) {
        mergeSortFun(array,0,array.length-1);
    }

    public static void mergeSortFun(int[] array,int left,int right) {
        if(left >= right) {
            return;
        }
        int mid = (left + right) / 2;

        //左子树递归
        mergeSortFun(array,left,mid);
        //右子树递归
        mergeSortFun(array,mid+1,right);
        megerNumSort(array,left,mid,mid+1,right);
    }

    private static void megerNumSort(int[] array,int begin1,int end1,int begin2,int end2) {
        int[] tmp = new int[end2-begin1+1];
        int i = 0;
        int left = begin1;
        while(begin1 <= end1 && begin2 <= end2) {
            if (array[begin1] > array[begin2]) {
                tmp[i++] = array[begin2++];
            }else {
                tmp[i++] = array[begin1++];
            }
        }

        if(begin1 <= end1) {
            while(begin1 <= end1) {
                tmp[i++] = array[begin1++];
             }
        }

        if(begin2 <= end2) {
            while(begin2 <= end2) {
                tmp[i++] = array[begin2++];
            }
        }

        for(int j = 0; j < i; j++) {
            array[j+left] = tmp[j];
        }

    }




    public static void mergeSortNot(int[] array) {
        int gap = 1;
        while(gap < array.length) {
            for(int i = 0; i < array.length; i = i+gap*2) {
                int left = i;
                int mid = left + gap - 1;
                if(mid >= array.length) {
                    mid = array.length - 1;
                }
                int right = mid + gap;
                if(right >= array.length) {
                    right = array.length - 1;
                }
                megerNumSort(array,left,mid,mid+1,right);
            }
            gap = gap * 2;
        }
    }


















































}
